<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use fast\Tree;

use app\api\model\wanlshop\Invoice;
use app\api\model\wanlshop\Goods;
use app\admin\model\User;

/**
 * WanlShop店铺接口
 */
class Tickets extends Api
{
    protected $noNeedLogin = ['getShopInfo'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\api\model\wanlshop\Shop');
    }

    /**
     * 获取商家列表
     */
    public function get_shop_list()
    {
        $key = input('post.key');

        $list = model('app\api\model\wanlshop\Shop')->whereLike("shopname", "%{$key}%")->select();

        $this->success("查询成功", $list);
    }

    /**
     * 保存商家发票
     */
    public function save_invoice()
    {
        $shopid = input('post.shopid');
        $img = input('post.img');
        $price = input('post.price');

        if (!$shopid) {
            $this->error("请选择平台商户");
        }

        if (!$img) {
            $this->error("请上传发票");
        }

        if (!$price) {
            $this->error("请填写发票金额");
        }

        $res = Invoice::create([
            'uid' => $this->auth->id,
            'shopid' => $shopid,
            'img' => $img,
            'price' => $price
        ]);

        if ($res !== false) {
            $this->success("保存成功");
        } else {
            $this->error("保存失败");
        }
    }

    /**
     * 获取发票列表
     */
    public function get_invoice_list()
    {
        $uid = $this->auth->id;
        $page = input('post.page', 1);
        $limit = input('post.limit', 15);
        $sh = input('post.sh', ['>=', 0]);

        $list = Invoice::where(['uid' => $uid, 'sh' => $sh])->order("id desc")->paginate($limit)->toArray();

        foreach ($list['data'] as $lk => $lv) {
            $list['data'][$lk]['shopname'] = model('app\api\model\wanlshop\Shop')->where("id", $lv['id'])->value("shopname");
        }

        $this->success("查询成功", $list);
    }

    /**
     * 热更新
     */
    public function hot_update()
    {
        $site = config('site');

        $this->success("获取成功", ['wgt' => $site['app_wgt'], 'version' => $site['app_version']]);
    }

    /**
     * 查询可抵扣积分
     */
    public function get_use_scord()
    {
        $param = input('post.');
        $buys = $param['buys'];

        // $buys = [["gid"=>1,"buy_pirce"=>"5.00","buy_nums"=>"1"]];

        // $gids = input('post.gids');
        // $buy_nums = input('post.buy_nums');
        if (!$buys) {
            $this->success("获取成功", ['score' => 0, 'price' => 0]);
        }

        $site = config('site');
        $goods_mas_score_price = $site['goods_mas_score_price'];
        $tot = $score = 0;

        // 获取用户当前可用积分
        $user_score = User::where("id", $this->auth->id)->value('score');

        foreach ($buys as $bk => $bv) {
            $gid = $bv['gid'];
            $buy_price_one = $bv['buy_pirce'];
            $buy_nums = $bv['buy_nums'];
            $buy_pirce = $buy_price_one * $buy_nums;

            // 找到商品信息
            //$scores = Goods::where("id", $gid)->value('max_score');
            /*$goods = Goods::where("id", $gid)->find();
            if (!$goods || empty($goods->integral)) {
                $scores = $site['goods_mas_score'];
            } else {
                $scores = $goods->integral;
            }*/
            //只计算百分比
            $scores = $buy_pirce * $site['dhgz'] * $goods_mas_score_price / 100;

            // 计算可使用金额
            $use_pirce_max = round(($buy_pirce * $goods_mas_score_price / 100), 2);

            if ($user_score > 0 && $scores != 0) {
                if ($scores > 0) {
                    $cuse = ($user_score >= $scores) ? $scores : $user_score;
                    $price = $this->score_to_price($cuse);
                    if ($price > $use_pirce_max) {
                        $scores = $this->score_to_price($use_pirce_max, 2);
                        $score += $scores;
                        $tot += $use_pirce_max;
                        $user_score -= $scores;
                    } else {
                        $score += $cuse;
                        $tot += $this->score_to_price($cuse);
                        $user_score -= $cuse;
                    }
                } else {
                    // 不限制抵扣
                    $price = $this->score_to_price($user_score);
                    if ($price > $use_pirce_max) {
                        $scores = $this->score_to_price($use_pirce_max, 2);
                        $score += $scores;
                        $tot += $use_pirce_max;
                        $user_score -= $scores;
                    } else {
                        $score += $user_score;
                        $tot += $this->score_to_price($user_score);
                        $user_score -= $user_score;
                    }
                }
            }
        }

        $use = [
            'score' => number_format($score, 0, '.', ''),
            'price' => $tot
        ];

        $this->success("查询成功", $use);
    }

    /**
     * 积分兑换人民币
     * type 1-积分转人民币; 2-人民币转积分
     */
    private function score_to_price($score = 0, $type = 1)
    {
        $site = config('site');
        $dhgz = $site['dhgz'];
        if ($type == 1) {
            if ($dhgz > 0) {
                $price = round(($score / $dhgz), 2);
                return $price;
            } else {
                return 0;
            }
        } else {
            return floor(($score * $dhgz));
        }
    }
}
